package com.hsy.crm.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Print;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.print.PrintGetPo;
import com.hsy.crm.web.domain.po.print.PrintPagePo;
import com.hsy.crm.web.domain.vo.print.PrintInsertVo;
import com.hsy.crm.web.domain.vo.print.PrintPageVo;
import com.hsy.crm.web.mapper.PrintMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrintService extends BaseService<Print> {

    @Autowired
    PrintMapper printMapper;

    public Unified<PrintInsertVo> insertPrint(PrintInsertVo vo) {
        Unified<PrintInsertVo> unified = Unified.create(PrintInsertVo.class);
        vo.setPrintDefault(0);
        vo.setId(UuidUtils.getUuid());
        checkInsert(vo);

        printMapper.insertPrint(vo);
        unified.setData(vo);

        return unified;
    }

    public Unified<PrintInsertVo> updatePrint(PrintInsertVo vo) {
        Unified<PrintInsertVo> unified = Unified.create(PrintInsertVo.class);
        checkUpdate(vo);
        printMapper.updatePrint(vo);
        unified.setData(vo);
        return unified;
    }

    public Unified<Integer> setDefault(Print print) {
        Unified<Integer> unified = Unified.create(Integer.class);

        if( print.getId() == null || print.getBussType() == null ){
            throw new ServiceException("params is error");
        }

        printMapper.clearDefault(print);

        Print select = new Print();
        select.setId(print.getId());
        select = printMapper.selectById(select);
        if( !select.getCustomerId().equals(DEFAULT_CUSTOMER_ID) ){
            printMapper.setDefault(print);
        }

        return unified;
    }

    public Unified<Integer> deletePrint(Print print) {
        Unified<Integer> unified = Unified.create(Integer.class);

        Print select = new Print();
        select.setId(print.getId());
        select = printMapper.selectById(select);

        if( select.getCustomerId().equals(PrintService.DEFAULT_CUSTOMER_ID) ){
            throw new ServiceException(App.BussError.PRINT_DELETE.getCode());
        }else{
            this.delete(print);
        }

        return unified;
    }

    public Unified<PrintGetPo> getPrint(Print print) {
        Unified<PrintGetPo> unified = Unified.create(PrintGetPo.class);
        unified.setData(printMapper.getPrint(print));
        return unified;
    }

    public PageResult<PrintPagePo> page(PrintPageVo printPageVo) {
        Page page = PageHelper.startPage(printPageVo.getPage(),printPageVo.getLimit());
        PageResult<PrintPagePo> pageResult = new PageResult<>();

        List<PrintPagePo> rows = printMapper.page(printPageVo);
        transRows(rows);
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public static final String DEFAULT_CUSTOMER_ID = "001002006008";

    private void transRows(List<PrintPagePo> rows){
        for( PrintPagePo row : rows ){
            if( row.getCustomerId().equals(DEFAULT_CUSTOMER_ID) && hasStyleDefault(rows,row.getBussType())  ){
                row.setPrintDefault(0);
            }
        }
    }

    private boolean hasStyleDefault(List<PrintPagePo> rows,int bussType){
        for( PrintPagePo printPagePo : rows ){
            if( printPagePo.getBussType().intValue() == bussType
                    && !printPagePo.getCustomerId().equals(DEFAULT_CUSTOMER_ID)
                    && printPagePo.getPrintDefault() == 1 ){
                return true;
            }
        }
        return false;
    }

    private void checkInsert(PrintInsertVo vo){
        if( vo.getBussType() == null || vo.getJson() == null ){
            throw new ServiceException(App.BussError.PRINT_PARAMS.getCode());
        }
    }

    private void checkUpdate(PrintInsertVo vo){
        if( vo.getId() == null && vo.getBussType() == null || vo.getJson() == null ){
            throw new ServiceException(App.BussError.PRINT_PARAMS.getCode());
        }
    }

}
